package com.aaa.controller;

import com.aaa.entity.LeadingUsers;
import com.aaa.service.LeadingUsersService;
import com.aaa.util.DefaultMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 前台用户管理控制器
 */
@RestController
@RequestMapping("/leadingUsers")
public class LeadingUsersController {
    @Resource
    private LeadingUsersService leadingUsersService;
//    //保存商品图片文件夹的名称
//    public static final String IMG_PATH="D:/upload/";

    /**
     * 查询所有用户信息
     * @return
     */
    @PostMapping("/listData")
    public PageInfo<LeadingUsers> listData(@RequestParam(defaultValue = "") String keyword, Integer currentPage, Integer pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<LeadingUsers> leadingUsers = leadingUsersService.listAll(keyword);
        PageInfo pageInfo=new PageInfo(leadingUsers);
        return pageInfo;
    }

    /**
     *根据lid删除前台用户
     * @param lid
     * @return
     */
    @RequestMapping("/del")
    public DefaultMsg delete(Integer lid){
        DefaultMsg defaultMsg = leadingUsersService.delete(lid);
        return defaultMsg;
    }

    /**
     * 重置前台用户密码
     * @param leadingUsers
     * @return
     */
    @RequestMapping("/resetPasswords")
    public DefaultMsg resetPasswords(LeadingUsers leadingUsers){
        return leadingUsersService.resetPasswords(leadingUsers);
    }

    /**
     * 启用前台用户
     * @param leadingUsers
     * @return
     */
    @RequestMapping("/updateQi")
    public Integer updateQi(LeadingUsers leadingUsers){
        return leadingUsersService.updateQi(leadingUsers);
    }

    @RequestMapping("/updateJin")
    public Integer updateJin(LeadingUsers leadingUsers){
        return leadingUsersService.updateJin(leadingUsers);
    }

//    /**
//     * 图片上传的功能
//     * @param file
//     * @return
//     */
//    @RequestMapping("/upload")
//    public DefaultMsg upload(MultipartFile file){
//        DefaultMsg defaultMsg=new DefaultMsg();
//        System.out.println("图片上传被调用");
//        //使用UUID给文件生成唯一的文件名
//        String fileName = UUID.randomUUID().toString();
//        File targetFile=new File(IMG_PATH+fileName);
//        try {
//            file.transferTo(targetFile);
//        } catch (IOException e) {
//            e.printStackTrace();
//            defaultMsg.setSuccess(0);
//            defaultMsg.setMsg("文件上传失败");
//        }
//        defaultMsg.setTarget(fileName);
//        return defaultMsg;
//
//    }
}
